package com.hupu.jrs.ui.account

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.ui.login.LoginActivity
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.db.User

/**
 * Created by sll on 2016/3/10.
 */
class AccountActivity : BaseSwipeBackActivity(), AccountContract.View, AccountAdapter.OnItemClickListener {

    @Inject
    internal var mPresenter: AccountPresenter? = null
    @Inject
    internal var mAdapter: AccountAdapter? = null

    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null
    @BindView(R.id.recyclerView)
    internal var recyclerView: RecyclerView? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = true

    override fun initContentView(): Int {
        return R.layout.activity_account
    }

    override fun initInjector() {
        DaggerAccountComponent.builder()
                .applicationComponent(applicationComponent)
                .activityModule(activityModule)
                .accountModule(AccountModule())
                .build()
                .inject(this)
    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        mPresenter!!.attachView(this)
        initToolBar(toolbar)
        title = "账号管理"
        val layoutManager = LinearLayoutManager(this)
        recyclerView!!.layoutManager = layoutManager
        recyclerView!!.adapter = mAdapter
        mAdapter!!.setOnItemClickListener(this)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_account, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.action_add) {
            LoginActivity.startActivity(this)
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun renderUserList(users: List<User>) {
        mAdapter!!.bind(users)
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    override fun onAccountItemDelClicked(user: User?) {
        mPresenter!!.onAccountDelClick(user!!)
    }

    override fun onAccountItemClicked(user: User?) {
        mPresenter!!.onAccountClick(user!!)
        mPresenter!!.onAccountClick(user!!)
    }

    companion object {

        fun startActivity(mActivity: Activity) {
            val intent = Intent(mActivity, AccountActivity::class.java)
            mActivity.startActivity(intent)
        }
    }
}
